local ganglie = fk.CreateSkill {
  name = "klmou__ganglie",
}

Fk:loadTranslationTable{
  ["klmou__ganglie"] = "刚烈",
  [":klmou__ganglie"] = "当你受到1点伤害后，你可以发动一次判定，"..
  "<br>若为红色你选择一项：1.对伤害来源造成1点伤害，2.获得一个“伤”标记；"..
  "<br>若为黑色你选择一项：1.令伤害来源弃置两张牌，2.获得一个“拆”标记。"..
  "<br>你的“伤”和“拆”标记上限为1，出牌阶段每种标记各限一次，你可以弃置对应标记选择一名本局游戏对你造成过伤害的角色并执行对应操作：1.“伤”对其造成2点伤害；2.“迁”弃置其两张牌。",

  ["#klmou__ganglie-red"] = "对 %dest 造成1点伤害",
  ["#klmou__ganglie-red2"] = "对伤害来源造成1点伤害",
  ["#klmou__ganglie-black"] = "令 %dest 弃置两张牌",
  ["#klmou__ganglie-black2"] = "令伤害来源弃置两张牌",
  ["#klmou__ganglie-get"] = "获得一个“%arg”标记",
  ["#klmou__ganglie-discard"] = "刚烈：请弃置两张牌",
  ["#klmou__ganglie-active_discard"] = "刚烈：请弃置 %dest 的两张牌",
  ["@klmou__ganglie_shang"] = "伤",
  ["@klmou__ganglie_chai"] = "拆",
  ["#klmou__ganglie-active"] = "你可以弃置对应标记，选择一名本局游戏对你造成过伤害的角色并执行对应操作",
  ["#klmou__ganglie_shang"] = "“伤”对其造成2点伤害",
  ["#klmou__ganglie_chai"] = "“拆”弃置其两张牌",

  ["$klmou__ganglie1"] = "体发之仇，如何不报？",
  ["$klmou__ganglie2"] = "以牙还牙，以眼还眼！",
}

ganglie:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local tos = {}
  room.logic:getActualDamageEvents(#room.players, function (e)
    local damage = e.data
    if damage.from and damage.to == player and not table.contains(tos, damage.from.id) then
      table.insertIfNeed(tos, damage.from.id)
    end
  end, Player.HistoryGame)
  if #tos > 0 then
    room:setPlayerMark(player, "klmou__ganglie_tos", tos)
  end
end)

ganglie:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.to:hasSkill(ganglie.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addTableMarkIfNeed(data.to, "klmou__ganglie_tos", player.id)
  end,
})

ganglie:addEffect("active", {
  anim_type = "offensive",
  prompt = "#klmou__ganglie-active",
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  interaction = function (self, player)
   local all_choices = {"#klmou__ganglie_shang", "#klmou__ganglie_chai"}
   local choices = {}
   if player:getMark("@klmou__ganglie_shang") > 0 and player:getMark("klmou__ganglie1-phase") == 0 then
     table.insert(choices, "#klmou__ganglie_shang")
   end
   if player:getMark("@klmou__ganglie_chai") > 0 and player:getMark("klmou__ganglie2-phase") == 0 then
     table.insert(choices, "#klmou__ganglie_chai")
   end
   if #choices == 0 then return end
   return UI.ComboBox{ choices = choices , all_choices = all_choices }
  end,
  target_filter =  function (self, player, to_select, selected, selected_cards, card, extra_data)
   local tos = player:getTableMark("klmou__ganglie_tos")
   return #selected == 0 and table.contains(tos, to_select.id)
  end,
  can_use = function (self, player)
   return (player:getMark("@klmou__ganglie_shang") > 0 and player:getMark("klmou__ganglie1-phase") == 0) or
   (player:getMark("@klmou__ganglie_chai") > 0 and player:getMark("klmou__ganglie2-phase") == 0)
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if not self.interaction.data then return end
    if self.interaction.data == "#klmou__ganglie_shang" then
      room:removePlayerMark(player, "@klmou__ganglie_shang")
      room:setPlayerMark(player, "klmou__ganglie1-phase", 1)
      room:damage{
        from = player,
        to = target,
        damage = 2,
        skillName = ganglie.name,
      }
    elseif self.interaction.data == "#klmou__ganglie_chai" then
      room:removePlayerMark(player, "@klmou__ganglie_chai")
      room:setPlayerMark(player, "klmou__ganglie2-phase", 1)
      if not target:isNude() then
        local cards = room:askToChooseCards(player, {
          target = target,
          min = math.min(2, target:getHandcardNum()),
          max = math.min(2, target:getHandcardNum()),
          skill_name = ganglie.name,
          flag = "he",
          prompt = "#klmou__ganglie-active_discard::"..target.id,
        })
        if #cards > 0 then
          room:throwCard(cards, ganglie.name, target, player)
        end
      end
    end
  end,
})

ganglie:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function (self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ganglie.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = ganglie.name,
      pattern = ".|.|^nosuit",
    }
    room:judge(judge)
    local ch1 = ""
    local ch2 = "#klmou__ganglie-get:::"
    if judge.card.color == Card.Red then
      ch2 = ch2 .. "伤"
      ch1 = "#klmou__ganglie-red::"
      if not data.from then
        ch1 = "#klmou__ganglie-red2"
      end
    elseif judge.card.color == Card.Black then
      ch2 = ch2 .. "拆"
      ch1 = "#klmou__ganglie-black::"
      if not data.from then
        ch1 = "#klmou__ganglie-black2"
      end
    end
    if ch1 == "" then return end
    local choices = { ch1, ch2}
    if data.from ~= nil then
      choices[1] = ch1 .. data.from.id
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = ganglie.name,
    })
    if choice:startsWith(ch2) then
      if choice:endsWith("伤") then
        room:setPlayerMark(player, "@klmou__ganglie_shang", 1)
      elseif choice:endsWith("拆") then
        room:setPlayerMark(player, "@klmou__ganglie_chai", 1)
      end
    elseif choice:startsWith(ch1) then
      if not data.from then return end
      if choice:startsWith("#klmou__ganglie-red") then
        room:damage{
          from = player,
          to = data.from,
          damage = 1,
          skillName = ganglie.name,
        }
      elseif choice:startsWith("#klmou__ganglie-black") then
        if not data.from:isNude() then
          room:askToDiscard(data.from, {
            min_num = 2,
            max_num = 2,
            include_equip = true,
            skill_name = ganglie.name,
            cancelable = false,
            prompt = "#klmou__ganglie-discard",
          })
        end
      end
    end
  end,
})


return ganglie
